/*
 * @Author: jqm123 2289662078@qq.com
 * @Date: 2025-06-12 14:59:08
 * @LastEditors: jqm123 2289662078@qq.com
 * @LastEditTime: 2025-08-07 09:50:03
 * @FilePath: /anxinxiu_v3/src/router/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-06-12 14:59:08
 * @LastEditors: jqm123 2289662078@qq.com
 * @LastEditTime: 2025-08-07 08:47:17
 * @FilePath: /mobile_demo_v3/src/router/index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "@/layout/tabbar.vue";
import { isWechat, isZxcxAPP, zxcxGetUserInfo } from "@/utils/zxcxWebkit";
import { showToast } from "vant";
import { useUserStore } from "@/stores";

const routes = [
  {
    path: "/",
    name: "Layout",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/views/home.vue"),
        meta: {
          title: "首页",
          keepAlive: true,
        },
      },
      {
        path: "/store",
        name: "store",
        component: () => import("@/views/store.vue"),
        meta: {
          title: "门店",
          keepAlive: true,
        },
      },
      {
        path: "/serve",
        name: "serve",
        component: () => import("@/views/serve.vue"),
        meta: {
          title: "服务管家",
          keepAlive: true,
        },
      },
      {
        path: "/my",
        name: "my",
        component: () => import("@/views/my.vue"),
        meta: {
          title: "我的",
          keepAlive: true,
        },
      },
      {
        path: "/vip",
        name: "vip",
        component: () => import("@/views/vip.vue"),
        meta: {
          title: "会员",
          keepAlive: true,
        },
      },
    ],
  },
  {
    path: "/search",
    name: "search",
    component: () => import("@/pages/home/search.vue"),
    meta: {
      name: "搜索",
    },
  },
  {
    path: "/systemMessage",
    name: "systemMessage",
    component: () => import("@/pages/home/systemMessage.vue"),
    meta: {
      title: "服务通知",
    },
  },
  {
    path: "/addCar",
    name: "addCar",
    component: () => import("@/pages/home/addCar.vue"),
    meta: {
      name: "添加爱车",
    },
  },
  {
    path: "/baoyang",
    name: "baoyang",
    component: () => import("@/pages/home/baoyang.vue"),
    meta: {
      title: "保养",
    },
  },
  {
    path: "/chat",
    name: "chat",
    component: () => import("@/pages/home/chat.vue"),
    meta: {
      title: "安心修智能管家",
    },
  },
  {
    path: "/addWeiquan",
    name: "addWeiquan",
    component: () => import("@/pages/home/addWeiquan.vue"),
    meta: {
      title: "我要维权",
    },
  },
  {
    path: "/hotActive",
    name: "hotActive",
    component: () => import("@/pages/home/hotActive.vue"),
    meta: {
      name: "爆款活动",
    },
  },
  {
    path: "/serveDetail",
    name: "serveDetail",
    component: () => import("@/pages/serve/serveDetail.vue"),
    meta: {
      name: "服务详情",
    },
  },
  {
    path: "/customer",
    name: "customer",
    component: () => import("@/pages/serve/customer.vue"),
    meta: {
      name: "客服消息",
    },
  },
  {
    path: "/storeDetail",
    name: "storeDetail",
    component: () => import("@/pages/store/storeDetail.vue"),
    meta: {
      name: "门店详情",
    },
  },
  {
    path: "/allVipUse",
    name: "allVipUse",
    component: () => import("@/pages/vip/allVipUse.vue"),
    meta: {
      name: "所有适用门店",
    },
  },
  {
    path: "/404",
    name: "404",
    component: () => import("@/views/404.vue"),
    meta: {
      title: "404",
    },
  },
  {
    path: "/setting",
    name: "setting",
    component: () => import("@/pages/my/setting.vue"),
    meta: {
      name: "设置",
    },
  },
  {
    path: "/order",
    name: "order",
    component: () => import("@/pages/my/order.vue"),
    meta: {
      name: "全部订单",
    },
  },
  {
    path: "/orderDetail",
    name: "orderDetail",
    component: () => import("@/pages/my/orderDetail.vue"),
    meta: {
      name: "订单详情",
    },
  },
  {
    path: "/address",
    name: "address",
    component: () => import("@/pages/my/address.vue"),
    meta: {
      name: "收货信息",
    },
  },
  {
    path: "/addressDetail",
    name: "addressDetail",
    component: () => import("@/pages/my/addressDetail.vue"),
    meta: {
      name: "新增/编辑收货信息",
    },
  },
  {
    path: "/myWeiquan",
    name: "myWeiquan",
    component: () => import("@/pages/my/myWeiquan.vue"),
    meta: {
      name: "我的维权",
    },
  },
  {
    path: "/coupon",
    name: "coupon",
    component: () => import("@/pages/my/coupon.vue"),
    meta: {
      name: "我的券包",
    },
  },
  {
    path: "/makeSure",
    name: "makeSure",
    component: () => import("@/pages/store/makeSure.vue"),
    meta: {
      name: "确认订单",
    },
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach(async (to, from, next) => {
  if (from.path === "/") {
    let userStore = useUserStore();
    if (isZxcxAPP()) {
      let appInfo = await zxcxGetUserInfo();
      await userStore.login(JSON.parse(appInfo).userId);
      next();
    } else if (import.meta.env.VITE_MODE === "development") {
      // await userStore.login("7ceb2e44-da6a-4924-ad87-fd7a311d9841");
      await userStore.login("b47f15ac-09ef-11ee-b9d1-286ed489a937");

      next();
    } else if (to.path === "/hotActive" && isWechat()) {
    } else {
      showToast({
        message: "请在掌心长兴打开",
        duration: 0,
        forbidClick: true,
      });
      next();
    }
  } else {
    next();
  }
});

export default router;
